package cn.com.dhcc.adam.sort;

import java.util.Arrays;

public class InsertDirectSort {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		test1();
		//test2();
		
		//test3();
	}
	
	public static void test1(){
		int a[]={49,38,65,97,76,13,27,49,89,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51,143};  
	    int[] ra = InsertDirectSort.sort(a);
	    System.out.println(Arrays.toString(ra));
	    
	    int nums = 100;
	    int randInt[] = new int[nums];
	    for (int i = 0; i < nums; i++) {
			randInt[i] = (int) (Math.random()*nums);
		}
	    System.out.println(Arrays.toString(randInt));
	    int[] randIntSorted = InsertDirectSort.sort(randInt);
	    System.out.println(Arrays.toString(randIntSorted));
	    
	    String s = "chendong";
	    InsertDirectSort.sp(s);
	    System.out.println("in main: s = " + s);
	    
	    int i = 11;
	    InsertDirectSort.ip(i);
	    System.out.println("in main: i = " + i);
	}
	public static void test2(){
		int nums = 100;
	    int randInt[] = new int[nums];
	    for (int i = 0; i < nums; i++) {
			randInt[i] = (int) (Math.random()*nums);
		}
	    System.out.println(Arrays.toString(randInt));
	    int[] randIntSorted = InsertDirectSort.shellSort(randInt);
	    System.out.println(Arrays.toString(randIntSorted));
	    
	}
	
	/**
	 * 从小到大 排列
	 * @param a
	 * @return
	 */
	public static int[] sort(int[] a){

		if(a.length>=2){
			int[] sorted;// = new int[a.length];
			//System.arraycopy(a, 0, sorted, 0, a.length);
			sorted = Arrays.copyOf(a, a.length);    //拷贝数组副本
			int computedCount = 0;    //时间复杂度
			/**
			 * 开始排序算法
			 */
			int temp;
			for (int i = 1; i < sorted.length; i++) {
				for (int j = 0; j < i; j++) {
					computedCount++;
					if(sorted[j]>sorted[i]){
						temp = sorted[i];
						System.arraycopy(sorted, j, sorted, j+1, i-j);
						sorted[j] = temp;
						break;
					}else continue;
				}
			}
			System.out.println("数组长度：" +  sorted.length + ",循环次数=" + computedCount +"");
			return sorted;
		}else {
			return a;
		}
		
	}
	public static String sp(String sp){
		String ss = sp;
		ss = "cd";
		System.out.println("in sp(): sp = " + sp);
		return ss;
	}
	

	public static int ip(int ip){
		int ii = ip;
		ii = 999;
		System.out.println("in ip(): ip = " + ip);
		return ii;
	}
	/**
	 * 希尔排序
	 * @param a 待排序数组
	 * @return 排序完成的数组
	 */
	public static int[] shellSort(int[] a){
		if(a.length>=2){
			int[] cloneA = Arrays.copyOf(a, a.length);
			
			
			return cloneA;
		}else{
			return a;
		}
	}
	
	
	public static void test3(){
		int [] a = {49,38,65,97,76,13,27,49,89,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51,143};  
		
		System.out.println(Arrays.toString(a));
		int head = a[0];
		System.arraycopy(a, 1, a, 0, a.length-1);
		a[a.length-1] = head;
		System.out.println(Arrays.toString(a));
		
		
		System.out.println((int)(Math.ceil(8.6)));
	}
}
